package com.caiheng.api.controller.estate;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caiheng.api.constant.ResponseCode;
import com.caiheng.api.controller.platform.BaseController;
import com.caiheng.api.entity.estate.ReceptionDayMsg;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.service.estate.EstateUserService;
import com.caiheng.api.service.estate.ReceptionDayMsgService;
import com.caiheng.api.service.wxuser.UserService;
import com.caiheng.api.util.GsonResultBean;
import com.caiheng.api.util.Tools;
import com.vdurmont.emoji.EmojiParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value ="/reception")
public class ReceptionDayController  extends BaseController {

    @Resource
    private UserService userService;
    @Resource
    private ReceptionDayMsgService receptionDayMsgService;
    @Resource
    private EstateUserService estateUserService;


    /**
     *  添加接待信息
     * @param
     * @throws Exception
     */
    @PostMapping(value="/add")
    public JSONObject add(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            if(StrUtil.isEmpty(requestParam)){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"参数异常"),true,true);
            }
            logBefore(logger, "reception/add 接收的参数： "+requestParam);
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            String phone_num = jsonRequest.containsKey("phone_num") ? jsonRequest.getStr("phone_num") : "";
            String content = jsonRequest.containsKey("content") ? jsonRequest.getStr("content") : "";

            User user = userService.findById(user_id);
            if(user == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"当前用户不存在"),true,true);
            }

            if(user.getEstateId() == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"您未绑定小区"),true,true);
            }

            if(StrUtil.isNotEmpty(user.getPhoneNum())){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"您未绑定小区"),true,true);
            }

        }catch (Exception e){
            e.printStackTrace();
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            System.out.println(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        return result;
    }


    /**
     *  接待信息的 详情
     * @param
     * @throws Exception
     */
    @PostMapping(value="/detail")
    public JSONObject detail(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            if(StrUtil.isEmpty(requestParam)){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"参数异常"),true,true);
            }
            logBefore(logger, "reception/add 接收的参数： "+requestParam);
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            int msg_id = jsonRequest.containsKey("msg_id") ? jsonRequest.getInt("msg_id") : 0;

            User user = userService.findById(user_id);
            if(user == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"当前用户不存在"),true,true);
            }

            ReceptionDayMsg dayMsg = receptionDayMsgService.findById(msg_id);
            return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"当前用户不存在",dayMsg),true,false);
        }catch (Exception e){
            e.printStackTrace();
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            System.out.println(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        return result;
    }


    /**
     *  接待信息列表
     * @param
     * @throws Exception
     */
    @PostMapping(value="/list")
    public JSONObject list(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            if(StrUtil.isEmpty(requestParam)){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"参数异常"),true,true);
            }
            logBefore(logger, "reception/list 接收的参数： "+requestParam);
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            int estate_id = jsonRequest.containsKey("estate_id") ? jsonRequest.getInt("estate_id") : 0;

            User user = userService.findById(user_id);
            if(user == null){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"当前用户不存在"),true,true);
            }

//            EstateUser estateUser = estateUserService.getEstateByManagerUser(user_id);
//            if(estateUser == null){
//                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"暂无权限"),true,true);
//            }

            Map<String, Object> map = new HashMap<>();
            QueryWrapper<ReceptionDayMsg> qw = new QueryWrapper<>();
            if(estate_id  == 0){
                qw.eq("estate_id", user.getEstateId());
            }else{
                qw.eq("estate_id", estate_id);
            }
            List<ReceptionDayMsg> list = receptionDayMsgService.list(qw);
            if(CollUtil.isNotEmpty(list)){
                for (ReceptionDayMsg msg : list){
                    User userSend = userService.findById(msg.getUserId());
                    if(userSend!= null){
                        msg.setUserFaceUrl(userSend.getFaceUrl());
                        if(StrUtil.isNotEmpty(userSend.getNickName())){
                            msg.setUserNickName(EmojiParser.parseToUnicode(userSend.getNickName()));
                        }
                    }
                }
            }

            map.put("day_msg",list);

            return Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"查询成功",list.size(),map),false,false);
        }catch (Exception e){
            e.printStackTrace();
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.ERROR,"系统异常",e.getMessage()),true,true);
            System.out.println(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        return result;
    }



}
